﻿using System.Collections.Generic;
using System.Windows;
using System.Windows.Documents;
using System.Windows.Media;

namespace Utils.Wpf
{
	public class FlowDocumentExtended : FlowDocument
	{
		#region Attributes

		private Section _currentSection;
		private Paragraph _currentParagraph;
		private Run _currentRun;

		#endregion

		#region Properties

		public Section CurrentSection
		{
			get
			{
				return _currentSection;
			}
			set
			{
				if (value != _currentSection)
				{
					_currentSection = value;
					base.Blocks.Add(_currentSection);
				}
			}
		}

		public Paragraph CurrentParagraph
		{
			get
			{
				return _currentParagraph;
			}
			set
			{
				if (value != _currentParagraph)
				{
					_currentParagraph = value;
					this.CurrentSection.Blocks.Add(_currentParagraph);
				}
			}	
		}

		public Run CurrentRun
		{
			get
			{
				return _currentRun;
			}
			set
			{
				if (value != _currentRun)
				{
					_currentRun = value;
					this.CurrentParagraph.Inlines.Add(_currentRun);
				}
			}		
		}

		#endregion

		#region Public Methods

		public static Table CreateTable(int columnNumber, IEnumerable<Paragraph> contents)
		{
			Table table = new Table();
			TableRowGroup rowGroup = new TableRowGroup();

			table.BorderThickness = new Thickness(1.0);
			table.Background = Brushes.LightGray;

			int columnIndex = 0;
			TableRow row = new TableRow();
			IEnumerator<Paragraph> enumerator = contents.GetEnumerator();
			while (enumerator.MoveNext())
			{
				if (columnIndex > columnNumber - 1)
				{
					columnIndex = 0;
					rowGroup.Rows.Add(row);
					row = new TableRow();
					row.Cells.Add(new TableCell(enumerator.Current));
					columnIndex++;
				}
				else
				{
					columnIndex++;
					row.Cells.Add(new TableCell(enumerator.Current));
				}
			}

			if (rowGroup.Rows[rowGroup.Rows.Count - 1] != row) 
			{
				rowGroup.Rows.Add(row);
			}

			table.RowGroups.Add(rowGroup);

			return table;
		}

		public static Table CreateTable(IEnumerable<string> headerCaptions)
		{
			Table table = new Table();
			TableRowGroup headers = new TableRowGroup();
			TableRow headersRow = new TableRow();

			table.BorderThickness = new Thickness(1.0);

			foreach (string headerCaption in headerCaptions)
			{
				table.Columns.Add(new TableColumn());
				headersRow.Cells.Add(new TableCell(new Paragraph(new Bold(new Run(headerCaption)))));
			}

			headers.Rows.Add(headersRow);
			table.RowGroups.Add(headers);

			return table;
		}

		public static Run CreateColoredValue(decimal value)
		{
			Run result = new Run(value.ToString(".##"));
			result.Foreground = value > 0 ? Brushes.Green : Brushes.Red;

			return result;
		}

		public static Paragraph CreateColoredValue(string caption, decimal value)
		{
			Paragraph result = new Paragraph();
			result.Inlines.Add(caption);
			result.Inlines.Add(FlowDocumentExtended.CreateColoredValue(value));

			return result;
		}

		public static ListItem CreateListItem(string text)
		{
			return new ListItem(new Paragraph(new Run(text)));
		}

		public static TableCell CreateTableCell(string text)
		{
			return new TableCell(new Paragraph(new Run(text)));
		}

		#endregion
	}
}
